import { Configuration, App } from "@midwayjs/core/dist";
import * as koa from "@midwayjs/koa/dist";
import * as validate from "@midwayjs/validate";
import * as info from "@midwayjs/info";
import { join } from "path";
import { DefaultErrorFilter } from "./filter/default.filter";
import { NotFoundFilter } from "./filter/notfound.filter";
import { ReportMiddleware } from "./middleware/report.middleware";
import * as orm from "@midwayjs/typeorm";
import * as jwt from "@midwayjs/jwt";
import { JwtMiddleware } from "./middleware/jwt.middleware";

@Configuration({
  imports: [
    koa,
    validate,
    orm,
    jwt,
    {
      component: info,
      enabledEnvironment: ["local"],
    },
  ],
  importConfigs: [join(__dirname, "./config")],
})
export class MainConfiguration {
  @App("koa")
  mainApp: koa.Application;

  async onReady() {
    // add middleware
    this.mainApp.useMiddleware([ReportMiddleware, JwtMiddleware]);
    // add filter
    this.mainApp.useFilter([NotFoundFilter, DefaultErrorFilter]);
  }
}
